package com.dhgate.spu.analysis.init;

import java.io.File;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dhgate.spu.analysis.consts.SysConsts;
import com.dhgate.spu.analysis.dao.ConnectionPoolAbs;
import com.dhgate.spu.analysis.dao.JdbcDAO;

/**
 * @author: wangguodong
 * @create: 2014-2-13 下午3:29:24
 * @description:
 */
public class InitDbConfig {
	protected static final Logger logger = LoggerFactory.getLogger(JdbcDAO.class);

	/**
	 * 得到document 对象
	 * @param xmlFileName
	 * @return
	 * @throws Exception
	 */
	private static Document getDocument(String xmlFileName) throws Exception {
		SAXReader reader = new SAXReader();
		Document d = reader.read(new File(xmlFileName));
		return d;
	}

	/**
	 * 从XML配置文件中初始化系统信息
	 * 
	 */
	public static void init() throws Exception {
		try {
			logger.info("InitDbConfig begin");
			// 获得XML文档对象
			Document document = getDocument(SysConsts.CONST_DBXML_PATH);
			Element rootElement = document.getRootElement();

			Element searchdbElement = rootElement.element("SEARCHDB");

			SysConsts.CONST_SEARCHDB_DBDRIVER = searchdbElement.elementText("DBDRIVER");

			SysConsts.CONST_SEARCHDB_DBURL = searchdbElement.elementText("DBURL");

			SysConsts.CONST_SEARCHDB_DBUSERNAME = searchdbElement.elementText("DBUSERNAME");

			SysConsts.CONST_SEARCHDB_DBPASSWORD = searchdbElement.elementText("DBPASSWORD");

			SysConsts.Pool_SEARCHDB_IncrmentConns = Integer.parseInt(searchdbElement.elementText("INCREMENTCONNS"));

			SysConsts.Pool_SEARCHDB_InitConns = Integer.parseInt(searchdbElement.elementText("INITCONNS"));

			SysConsts.Pool_SEARCHDB_MaxConns = Integer.parseInt(searchdbElement.elementText("MAXCONNS"));

			SysConsts.Pool_SEARCHDB_WaitConn = Integer.parseInt(searchdbElement.elementText("WAITCONN"));

			SysConsts.DBURL_MAP.put("oracle", SysConsts.CONST_SEARCHDB_DBURL);
			SysConsts.CONNPOOLABSMAP.put(SysConsts.CONST_SEARCHDB_DBURL, new ConnectionPoolAbs(SysConsts.CONST_SEARCHDB_DBDRIVER, SysConsts.CONST_SEARCHDB_DBURL, SysConsts.CONST_SEARCHDB_DBUSERNAME, SysConsts.CONST_SEARCHDB_DBPASSWORD, SysConsts.Pool_SEARCHDB_InitConns, SysConsts.Pool_SEARCHDB_IncrmentConns,
					SysConsts.Pool_SEARCHDB_MaxConns, SysConsts.Pool_SEARCHDB_WaitConn));

			Element dhprodbElement = rootElement.element("DHPRODB");

			SysConsts.CONST_DHPRODB_DBDRIVER = dhprodbElement.elementText("DBDRIVER");

			SysConsts.Pool_DHPRODB_IncrmentConns = Integer.parseInt(dhprodbElement.elementText("INCREMENTCONNS"));

			SysConsts.Pool_DHPRODB_InitConns = Integer.parseInt(dhprodbElement.elementText("INITCONNS"));

			SysConsts.Pool_DHPRODB_MaxConns = Integer.parseInt(dhprodbElement.elementText("MAXCONNS"));

			SysConsts.Pool_DHPRODB_WaitConn = Integer.parseInt(dhprodbElement.elementText("WAITCONN"));

//			List<Element> dhprodbDbinfoElementList = dhprodbElement.elements("DBINFO");
//			for (int i = 0; i < dhprodbDbinfoElementList.size(); i++) {
//				String key = dhprodbDbinfoElementList.get(i).attributeValue("key");
//				String username = dhprodbDbinfoElementList.get(i).attributeValue("username");
//				String passwd = dhprodbDbinfoElementList.get(i).attributeValue("passwd");
//				String url = dhprodbDbinfoElementList.get(i).attributeValue("url");
//				SysConsts.MYSQL_TABLE_KEY_LIST.add(key);
//				SysConsts.DBURL_MAP.put(key, url);
//				if (!SysConsts.CONNPOOLABSMAP.containsKey(url)) {
//					SysConsts.CONNPOOLABSMAP.put(url, new ConnectionPoolAbs(SysConsts.CONST_DHPRODB_DBDRIVER, url, username, passwd, SysConsts.Pool_DHPRODB_InitConns, SysConsts.Pool_DHPRODB_IncrmentConns, SysConsts.Pool_DHPRODB_MaxConns, SysConsts.Pool_DHPRODB_WaitConn));
//				}
//
//			}
			
			logger.info("InitDbConfig end");

		} catch (Exception e) {
			logger.info("error:"+e);
			throw new Exception("error:"+e);
		}
	}
}
